const util = require('../../utils/utils.js');

var refresh = false; // 是否在刷新

Page({
   data: {
      winWidth: 0,      // 设备屏幕宽
      winHeight: 0,     // 设备屏幕高

      datalist: [],

      dataListDateCurrent: 0,       // 请求参数：日期
      dataListDateCount: 0,         // 请求次数限制

      loadmorehidden: false,     // 上拉加载更多 动画
      refreshhidden: false       // 下拉刷新动画
   },
   onLoad: function (options) {

      var that = this;

      // 获取设备宽高
      wx.getSystemInfo({
         success: function (res) {
            that.setData({
               winWidth: res.windowWidth,
               winHeight: res.windowHeight
            });
         }
      });

      // 接口请求
      wx.request({
         url: 'https://news-at.zhihu.com/api/4/news/latest',
         method: 'get',
         data: {},
         header: { "Content-Type": "application/json" },
         success: function (res) {

            var arr = res.data;
            var format = util.getFormatDate(arr.date);
            arr["dateDay"] = format.dateDay;

            var list = that.data.datalist;

            that.setData({
               datalist: list.concat(arr),
               dataListDateCurrent: arr.date,
               dataListDateCount: 1
            });
         }
      });

   },

   // 下拉 刷新列表(滚到顶部)
   onPullDownRefresh: function (e) {

      if (!refresh) {
         var that = this;
         refresh = true;

         // 显示刷新动画
         that.setData({
            refreshhidden: false
         })

         wx.request({
            url: 'https://news-at.zhihu.com/api/4/news/latest',
            method: 'get',
            data: {},
            header: { "Content-Type": "application/json" },
            success: function (res) {

               // 数据加载完成后 延迟隐藏loading
               setTimeout(function () {
                  wx.clearStorageSync(); // 清空数据

                  // 重置列表为空
                  that.setData({
                     datalist: []
                  })

                  refresh = false;

                  var arr = res.data;
                  var format = util.getFormatDate(arr.date);
                  arr["dateDay"] = format.dateDay;

                  var list = that.data.datalist;

                  that.setData({
                     datalist: list.concat(arr),
                     dataListDateCurrent: arr.date,
                     dataListDateCount: 1,
                     refreshhidden: false

                  });

               }, 2000);
            }
         });

      } else {
         console.log("正在刷新...");
      }
   },

   // 上拉 加载更多(滚到底部)
   onReachBottom: function (e) {

      var that = this;

      // 加载更多 loading
      that.setData({
         loadmorehidden: false
      })

      var currentDate = this.data.dataListDateCurrent; // 请求参数：日期

      // 限制加载次数
      if (this.data.dataListDateCount >= 3) {
         that.setData({ // 加载更多 loading
            loadmorehidden: true
         });
      } else {
         // 记录请求日期参数，避免滚动到底部事件，多次触发相同请求
         if (currentDate == wx.getStorageSync("currentDate")) {
            console.log(currentDate + "已发起请求!");
         } else {
            wx.request({
               url: 'https://news-at.zhihu.com/api/4/news/before/' + currentDate,
               method: 'get',
               data: {},
               header: { "Content-Type": "application/json" },
               success: function (res) {

                  var arr = res.data;
                  var format = util.getFormatDate(arr.date);
                  arr["dateDay"] = format.dateDay;

                  var list = that.data.datalist;

                  that.setData({
                     datalist: list.concat(arr),
                     dataListDateCurrent: arr.date,
                     dataListDateCount: that.data.dataListDateCount + 1
                  });
               }
            });
         }
      }
   },
   onReady: function (e) {
      console.log("onReady 清空缓存！");
      wx.clearStorageSync();
   }
})